package com.zcgame.customview.fresh

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.zcgame.base.utils.DensityUtil
import kotlin.math.min

/**
 *@author:huangLJ
 *@date:2021/5/10 19:25
 */
class MaterialHeader @JvmOverloads constructor(context: Context,
                                               attributeSet: AttributeSet? = null,
                        def:Int = 0): View(context,attributeSet,def),PullHeader {

    private val config = PullHeader.DefaultHeaderConfig()
    private var mScale = 1f
    private val mDrawable = MaterialProgressDrawable(context,this)
    init {
        mDrawable.setBackgroundColor(Color.WHITE)
        mDrawable.callback = this
        mDrawable.showArrow(true)
        mDrawable.alpha = 255
    }

    /**
     * 应该可以理解为重绘吧
     */
    override fun invalidateDrawable(drawable: Drawable) {
        if(drawable == mDrawable){
            invalidate()
        }else{
            super.invalidateDrawable(drawable)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        if(canvas != null){
            //保存后的画布，但是没有刷新，后面所有的操作都是基于保存后的画布进行的
            val saveCount = canvas.save()
            val bounds = mDrawable.bounds
            val l = paddingLeft + (measuredWidth - mDrawable.intrinsicWidth) / 2
            val offset = measuredHeight / 2 - mDrawable.intrinsicHeight / 2
            canvas.translate(l.toFloat(), (paddingTop + offset).toFloat())
            canvas.scale(mScale,mScale,bounds.exactCenterX(),bounds.exactCenterY())
            mDrawable.draw(canvas)
            canvas.restoreToCount(saveCount)
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var padding = paddingBottom+paddingTop
        if(padding == 0){
            padding = DensityUtil.dip2px(16f)
        }
        val heightMeasureSpecs = MeasureSpec.makeMeasureSpec(mDrawable.intrinsicHeight+padding,MeasureSpec.EXACTLY)
        super.onMeasure(widthMeasureSpec, heightMeasureSpecs)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        val size = mDrawable.intrinsicWidth
        mDrawable.setBounds(0,0,size,size)
    }



    override fun createHeaderView(refreshLayout: PullRefreshLayout): View {
        return this
    }

    override fun getConfig(): PullHeader.HeaderConfig {
        return config
    }

    override fun needCoverContent(): Boolean {
        return false
    }

    override fun onPullBegin(refreshView: PullRefreshLayout) {
        mDrawable.alpha = 255
    }

    override fun onPositionChange(refreshView: PullRefreshLayout, status: Byte, dy: Int, currentDistance: Int) {
        val totalDistance = config.totalDistance(refreshView, this)
        val percent = min(1f, 1.0f * currentDistance / totalDistance)
        if (status == PullRefreshLayout.PULL_STATUS_TOUCH_MOVE) {
            mDrawable.showArrow(true)
            val strokeStart = percent * 0.8f
            mDrawable.setStartEndTrim(0f, min(0.8f,strokeStart))
            mDrawable.setArrowScale(min(1f,percent))
            val fl = (-0.25f + 0.4f * percent + percent * 2) * .5f
            mDrawable.setProgressRotation(fl);
            invalidate()
        }
    }

    override fun onRefreshing(refreshView: PullRefreshLayout) {

        mDrawable.start()
    }

    override fun onReset(refreshView: PullRefreshLayout, pullRelease: Boolean) {
        mScale = 1f
        mDrawable.stop()
    }

    override fun onPullRefreshFinish(pullRefreshLayout: PullRefreshLayout) {

        mDrawable.stop()
    }
}